package com.forestar.mapControl.utils.mapConvert.task;

import android.graphics.Bitmap;
import android.graphics.Rect;

import com.forestar.mapControl.mapview.CacheCanvas;
import com.forestar.mapControl.mapview.multitouch.TestTextureViewRefreshListener;
import com.forestar.mapControl.utils.LogD;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * created by  xzk
 * 2020/8/5   12:00
 * 说明:  单层全屏彩色瓦片跨边界绘制
 */
public class DrawForColorTilesTask extends DrawForTask {

    public DrawForColorTilesTask(TestTextureViewRefreshListener refreshListener,
                                 AtomicInteger hasTaskNum,CacheCanvas targetCanvas) {
        super(refreshListener, hasTaskNum);
        this.targetCanvas = targetCanvas;
    }

    @Override
    public int drawBitmapTask() {
        millSum=new ArrayList<>();
        int startX=-(widthScreen/2);
        int startY=-(heightScreen/2);
        //计算一个屏幕需要多少张瓦片  上下左右
        offsetNum = widthScreen*2/tileSizeForShow;
        offsetNumY = heightScreen*2/tileSizeForShow;
        LogD.d("一共需要多少张瓦片:"+offsetNum+"x"+offsetNumY+"="+(offsetNum*offsetNumY));

        Rect rect = new Rect(startX,startY,startX+tileSizeForShow,startY+tileSizeForShow);
        int i= 1-offsetNum;
//            Rect sourceRect = new Rect(0,0,tileSizeForShow*5,tileSizeForShow*5);

        if(null == targetCanvas ){
            return 0;
        }

        int allNum = 0;
        int nowNum = 0;
        //总共1层
        long startForCanvasMill =0;
        long endForCanvasMill =0;
        for (int j = 0; j < bitmapTempList.size(); j++) {
            Bitmap bitmap = bitmapTempList.get(j);
            if (null == bitmap || bitmap.isRecycled()) {
                continue;
            }
            if(startForCanvasMill<1){
                startForCanvasMill = System.currentTimeMillis();
            }
            targetCanvas.getCanvas().drawBitmap(bitmap, null, rect, tempPaint);
            targetCanvas.getCanvas().drawRect(rect.left, rect.top, rect.right, rect.bottom, boxPaint);
            allNum++;

            nowNum++;
            if(nowNum!= 0 && nowNum%1000==0){
                if(endForCanvasMill<1){
                    endForCanvasMill = System.currentTimeMillis();
                }
                LogD.d("瓦片文件数量：本次绘制单层的时间："+(endForCanvasMill-startForCanvasMill));
                millSum.add("本次绘制单层的时间："+(endForCanvasMill-startForCanvasMill));
                startForCanvasMill =0;
                endForCanvasMill =0;
                //初始化
                i = 1-offsetNum;
                rect = new Rect(startX,startY,startX+tileSizeForShow,startY+tileSizeForShow);
            }else{
                //在当前图层绘制
                if (i % offsetNum != 0) {
                    rect.left = rect.left + tileSizeForShow;
                    rect.right = rect.right + tileSizeForShow;
                    rect.top = rect.top;
                    rect.bottom = rect.bottom;
                } else {
                    rect.left = startX;
                    rect.right = startX+tileSizeForShow;
                    rect.top = rect.top + tileSizeForShow;
                    rect.bottom = rect.bottom + tileSizeForShow;
                }
                i++;
            }
        }
        return allNum;
    }

}
